package me.tonywang.common.controller;

import me.tonywang.security.access.ProtectedUrlSecurityMetadataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Controller
public class DefaultController {

    @GetMapping(name="xxxx",path="/authenticated")
    public String authenticated() {
        return "/authenticated";
    }

    @GetMapping(name="xxxx", path="/thymeleaf")
    public String thymeleaf(HttpSession session ,HttpServletRequest request) {
        request.setAttribute("requestKey" ,"requestValue");
        session.setAttribute("sessionKey" ,"sessionValue");
        session.getServletContext().setAttribute("applicationKey" ,"applicationValue");
        return "/thymeleaf";
    }


    @PreAuthorize("@webSecurity.check(authentication)")
    @GetMapping({"/elAccessWeb" })
    @ResponseBody
    public String elAccessWeb() {
        return "elAccessWeb";
    }

     @ApiIgnore
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String index() {
        return "Hello World";
    }

    @GetMapping({"/home", "/"})
    public String home() {
        return "/home";
    }

    @GetMapping("/admin")
    public String admin() {
        return "/admin";
    }

    @GetMapping("/user")
    public String user() {
        return "/user";
    }

    @GetMapping("/about")
    public String about() {
        return "/about";
    }

    @GetMapping("/login")
    public String login() {
        return "/login";
    }

    @GetMapping("/403")
    public String error403() {
        return "/error/403";
    }


    @Autowired
    ProtectedUrlSecurityMetadataSource metadataSource;


    @GetMapping("/access/clear")
    public String clear() {
        metadataSource.reload();
        return "/home";
    }



}
